import util.StringUtil;

import java.util.ArrayList;

/**
 * Created by gamy on 2017/7/16.
 */
public class ConvertSQLToClass {

    public String convert(ArrayList<TableProperty> list) {
        String s = "";
        for (TableProperty tp : list) {
            if (ignoreTable(tp.tableName)) {
                continue;
            }
            s += handleTableNameBegin(tp);
            for (TableProperty.TableColumn column : tp.columns) {
                s += getColumnDefines(column);
            }

            s += "\n";

            for (TableProperty.TableColumn column : tp.columns) {
                s += getSetterMethod(column);
                s += getGetterMethod(column);
                s += "\n";
            }

            s += getToStringMethod(tp);
            s += "\n";
            s += handleTableNameEnd(tp);
            StringUtil.writeFile(s, tp.classFilePath);
            s = "";
        }

        return s;
    }

    private String handleTableNameBegin(TableProperty property) {
        String s = "";
//        s += "//" + property.classFilePath + "\n";
        s += "package " + property.packageName + ";\n";
        s += "import com.st.dao.base.STObject;\n\n\n";
        s += "/**\n" +
                " * Created by gamy on 2017/7/16.\n" +
                " */\n\n";

        s += "public class " + property.tableName + " extends STObject {\n\n";
        return s;
    }

    private String handleTableNameEnd(TableProperty property) {
        return "}\n";
    }

    private boolean ignoreTable(String str) {
        if (str == null || str == "" || str.contains("Index")) {
            return true;
        }
        ;
        return false;
    }

    private String getColumnDefines(TableProperty.TableColumn column) {
        return "    private " + column.type + " " + column.property + ";\n";
    }


    private String getGetterMethod(TableProperty.TableColumn column) {
        String s = "";
        s += "    public " + column.type + " get" + StringUtil.underline2Camel(column.column, false) + "() {\n" +
                "        return " + column.property + ";\n" +
                "    }\n";

        return s;
    }

    private String getSetterMethod(TableProperty.TableColumn column) {
        String s = "";
        s += "    public void set" + StringUtil.underline2Camel(column.column, false) + "(" + column.type + " " + column.property + ") {\n" +
                "        this." + column.property + " = " + column.property + ";\n" +
                "    }\n";

        return s;

    }

    private String getToStringMethod(TableProperty table) {
        String mid = "";

        for (int i = 0; i < table.columns.size(); i++) {
            TableProperty.TableColumn column = table.columns.get(i);
            if (i == 0) {
                mid += "\t\t\t\t+ \"" + column.property + ": \" + " + column.property;
            } else {
                mid += " + \",\"\n\t\t\t\t+ \"" + column.property + ": \" + " + column.property;
            }
        }
        String s = "";
        s += "    @Override\n" +
                "    public String toString() {\n" +
                "        return \"{\"\n" + mid + "\n\t\t\t\t+ \"}\";" +
                "\n\t}";

        return s;

    }
}
